package com.example.syl.basaimovie;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.syl.basaimovie.api.API;
import com.example.syl.basaimovie.database.OpenHelp;
import com.example.syl.basaimovie.myTask.WelcomeTask;
import com.example.syl.basaimovie.myinterface.MyInterface;
import com.squareup.picasso.Picasso;

import java.util.List;

public class WelcomeMainActivity extends AppCompatActivity implements MyInterface.WelcomeBackList {
    private ImageView imageView;
    private SQLiteDatabase db;
    private TextView time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_main);
        imageView= (ImageView) findViewById(R.id.welcome_iv);
        time= (TextView) findViewById(R.id.timer_tv);
        OpenHelp myData=new OpenHelp(this,"movie.db");
        db=myData.getReadableDatabase();
        //执行异步任务
        new WelcomeTask(this).execute(API.WelcomeUse);
        final MyCount myCount=new MyCount(4000,1000);//创建一个倒计时，总时长为4秒，间隔1秒
        myCount.start();//开启倒计时
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCount.cancel();//点击之后跳转
                startActivity(new Intent(WelcomeMainActivity.this,MainActivity.class));
                finish();
            }
        });
    }

    class MyCount extends CountDownTimer{

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }


        //倒计时结束时调用
        @Override
        public void onFinish() {
            Intent intent=new Intent(WelcomeMainActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
       /* 每间隔countDownInterval会调用一次
       * millisUntilFinished 已经过去了多长时间*/
        @Override
        public void onTick(long millisUntilFinished) {
            time.setText("跳过"+millisUntilFinished/1000+"s");
        }
    }
    @Override
    public void welcomeBackList(List<String> list) {
       Cursor cusor = db.rawQuery("select * from welcome", null);
        while (cusor.moveToNext()) {
            int notes = Integer.parseInt(cusor.getString(cusor.getColumnIndex("notes")));
            if (notes == 1) {//这个地方出错，notes=0应该为notes=1
                Picasso.with(this).load(list.get(0)).into(imageView);
                Log.e("TTTTT",list.get(0));
                db.execSQL("update welcome set notes=1 where notes='0'");
            } /*else {
                Picasso.with(this).load(list.get(1)).into(imageView);
                db.execSQL("update welcome set notes=0 where notes='1'");
            }*/
        }
    }

}
